package day20170416.io.test;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Test;

public class ReaderWriterStream {
	@Test
	public void testReaderWriter() throws Exception {
		readerWriteStream("C:\\Users\\Administrator\\Desktop\\1.txt", "2.txt");
	}
	
	public void readerWriteStream(String src,String dest){
		//选择流
		FileReader fr = null;
		FileWriter fw = null;
		try {
			//文件路径
			File in = new File(src);
			File out = new File(dest);
			//将文件路径装进管道
			fr = new FileReader(in);
			fw = new FileWriter(out);
			char[] ch = new char[24];
			int len;
			//每次读取下一个下标，如果等于-1，代表读取完成
			while((len = fr.read(ch)) != -1){
				//写入从0下标开始
				fw.write(ch, 0, len);
				//刷新
				fw.flush();
			}
		}catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(fr != null){
				try {
					fr.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(fw != null){
				try {
					fw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
